var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var session = require('express-session');

var app = express();

//session初始化
var SessionStore = require("express-mysql-session");

app.use(session({
    key: "love_nature_service_session",
    secret: "love_nature_service_session",
    resave: true,
    saveUninitialized: true,
    name: "asid",   //这里的name值得是cookie的name，默认cookie的name是：connect.sid
    cookie: {
        maxAge: 1000 * 3600 * 24 * 365
    },
    store: new SessionStore({
        host: '0.0.0.0',
        port: 3306,
        user: 'root',
        password: 'Yinhangzhangqi@2014',
        database: 'love_nature_service_session'
    }  )
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

//添加自定义方法的拦截器
app.use(function (req, res, next) {
    var _res_json = res.json;
    res.json = function (data, code, message) {
        _res_json.call(res, {
            data: data == undefined || data == null ? {} : data,
            code: code == undefined || code == null ? 0 : code,
            message: message == undefined || message == null ? "" : message
        });
    };
    //包装结束
    next();
});


//权限拦截器

var xhrNeedLogin = function (req, res, next) {
    if(req.session.hasOwnProperty("user"))
    {
        next();
    }
    else
    {
        res.json(null, 14);
    }
};

app.all("/qiniu/get_token", xhrNeedLogin);
app.all('/article/create', xhrNeedLogin);
app.all('/article/set_topic/:articleId', xhrNeedLogin);
app.all('/article/:id', xhrNeedLogin);
app.all('/article/list/:topicId', xhrNeedLogin);
app.all('/article/create_draft', xhrNeedLogin);
app.all('/article/update_draft/:id', xhrNeedLogin);
app.all('/article/publish/:id', xhrNeedLogin);
app.all('/article/draft/:type', xhrNeedLogin);

app.all('/comment/reply', xhrNeedLogin);
app.all('/comment/reply_list/:replyId', xhrNeedLogin);

app.all('/fav/article/:id', xhrNeedLogin);
app.all('/fav/article/:id/cancel', xhrNeedLogin);
app.all('/fav/article/:id', xhrNeedLogin);
app.all('/fav/list', xhrNeedLogin);

app.all('/message/have_read', xhrNeedLogin);
app.all('/message/inbox/unread', xhrNeedLogin);
app.all('/message/inbox/question_list', xhrNeedLogin);
app.all('/message/inbox/answer_list', xhrNeedLogin);
app.all('/message/inbox/reply_list', xhrNeedLogin);

app.all('/reply/create', xhrNeedLogin);
app.all('/reply/good/:id', xhrNeedLogin);
app.all('/reply/:id', xhrNeedLogin);
app.all('/reply/list/:articleId', xhrNeedLogin);

app.all('/topic/follow/list', xhrNeedLogin);
app.all('/topic/follow/update', xhrNeedLogin);

app.all('/user/set_user_state', xhrNeedLogin);
app.all('/user/update_avatar', xhrNeedLogin);
app.all('/user/update_nickname', xhrNeedLogin);
app.all('/user/logout', xhrNeedLogin);

app.use('/', require('./routes/index'));
app.use('/user', require('./routes/user'));
app.use('/article', require('./routes/article'));
app.use('/topic', require('./routes/topic'));
app.use('/qiniu', require('./routes/qiniu'));
app.use('/reply', require('./routes/reply'));
app.use('/message', require('./routes/message'));
app.use('/aliyunoss', require('./routes/aliyunoss'));
app.use('/search', require('./routes/search'));
app.use('/comment', require('./routes/comment'));
app.use('/fav', require('./routes/fav'));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
